package com.bocai.dahuatech.ui.frag;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.SolutionListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseFragment;
import com.bocai.dahuatech.ui.activity.NewsDetailActivity;
import com.bocai.dahuatech.ui.adapter.SolutionAdapterHolder;
import com.bocai.dahuatech.ui.view.LoadMoreRecyclerView;
import com.bocai.dahuatech.utils.AESUtils;
import com.google.gson.Gson;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 解决方案
 */
@SuppressLint("ValidFragment")
public class SolutionFragment extends BaseFragment {
    private View view;
    @Bind(R.id.srl_solution)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.rev_solution_list)
    LoadMoreRecyclerView mRecyclerView;
    private SolutionAdapterHolder adapter;
    private SolutionListBean bean;
    ArrayList<SolutionListBean.ContentEntity> datas=new ArrayList<>();
    int pager=1;
    int mState;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.restaurantfrag, container, false);
        initView(view);
        return view;
    }
    public SolutionFragment(int state){
        mState=state;
    }

    /** Fragment当前状态是否可见 */
    protected boolean isVisible;


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(getUserVisibleHint()) {
            if(!isVisible){
                getData();
                isVisible = true;
            }
        }
    }
    private void initView(View view) {
        ButterKnife.bind(this, view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter=new SolutionAdapterHolder(getActivity(),datas);
//        mRecyclerView.setAutoLoadMoreEnable(true);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pager = 1;
                getData();
            }
        });
        mRecyclerView.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                pager++;
                getData();
            }
        });
        initEvent();
//        getData();
    }
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            Log.e("erro", e.toString());
            showToast(R.string.neterror);
            hideLoading();
            swipeRefreshLayout.setRefreshing(false);
        }

        @Override
        public void onNext(BaseBean baseBean) {
            hideLoading();
            swipeRefreshLayout.setRefreshing(false);

            if(baseBean.getReturnNo().equals("1"))
            {
                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                bean=new Gson().fromJson(result,SolutionListBean.class);
                if(pager==1){
                    adapter=new SolutionAdapterHolder(getActivity(),bean.getContent());
                    mRecyclerView.setAdapter(adapter);
                }else{
                    adapter.addDatas(bean.getContent());
                }
//
//                datas.addAll(bean.getContent());
//                adapter.notifyDataSetChanged();

                if(bean.getContent().size()<20){

                    mRecyclerView.notifyMoreFinish(false);
                }else{
                    mRecyclerView.notifyMoreFinish(true);
                }
            }else
            {
                showError(baseBean.getReturnInfo());
            }

        }
    };
    private void getData() {
        showLoading();
        getTestApi().getSolutionList(mState, pager, 20)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    private void initEvent() {

        adapter.SetOnItemClickListener(new SolutionAdapterHolder.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(), NewsDetailActivity.class);
                SolutionListBean.ContentEntity data=bean.getContent().get(position);
                intent.putExtra("data",data);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
    @Override
    public void onResume() {
        super.onResume();
    }


}
